#include "main.h"
#include "command.h"
#include "login.h"
#include <mysql/mysql_com.h>
/* Usage:  */
void *threadFunc1(void *argv){
    shareRes_t *pShareRes=(shareRes_t *)argv;
    int socketFd= socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(socketFd,-1,"socket");

    struct sockaddr_in serverAddr;
    serverAddr.sin_family=AF_INET;
    serverAddr.sin_port=htons(12349);
    /* inet_pton(AF_INET,"127.0.0.1",(void *)&serverAddr.sin_addr); */
    inet_pton(AF_INET,"192.168.56.129",(void *)&serverAddr.sin_addr);

    int connectRet=connect(socketFd,(struct sockaddr*)&serverAddr,sizeof(serverAddr));
    ERROR_CHECK(connectRet,-1,"connect");
    // 开始验证
    int nameLen=strlen(pShareRes->name);
    commandType ret=TOK;
    send(socketFd,&ret,sizeof(int),0);
    send(socketFd,&nameLen,sizeof(int),0);
    send(socketFd,pShareRes->name,nameLen,0);
    send(socketFd,pShareRes->token,41,0);
    printf("tk=%s\n",pShareRes->token);
    recv(socketFd,&ret,sizeof(ret),MSG_WAITALL);
    if(ret==ERROR){
        printf("验证失败\n");
    }
    printf("传输线程的fd=%d\n",socketFd);
    commandReturn(socketFd,pShareRes->fileName,PUTS);
    while(1){
        sleep(1);
    }
}
void *threadFunc2(void *argv){
    shareRes_t *pShareRes=(shareRes_t *)argv;
    int socketFd= socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(socketFd,-1,"socket");

    struct sockaddr_in serverAddr;
    serverAddr.sin_family=AF_INET;
    serverAddr.sin_port=htons(12349);
    /* inet_pton(AF_INET,"127.0.0.1",(void *)&serverAddr.sin_addr); */
    inet_pton(AF_INET,"192.168.56.129",(void *)&serverAddr.sin_addr);

    int connectRet=connect(socketFd,(struct sockaddr*)&serverAddr,sizeof(serverAddr));
    ERROR_CHECK(connectRet,-1,"connect");
    // 开始验证
    
    commandType ret=TOK;
    int nameLen=strlen(pShareRes->name);
    send(socketFd,&ret,sizeof(int),0);
    send(socketFd,&nameLen,sizeof(int),0);
    send(socketFd,pShareRes->name,nameLen,0);
    send(socketFd,pShareRes->token,41,0);
    recv(socketFd,&ret,sizeof(ret),MSG_WAITALL);
    if(ret==ERROR){
        printf("验证失败\n");
    }
    printf("通过验证\n");
    commandReturn(socketFd,pShareRes->fileName,GETS);
}
int main(){              
    
   
    int socketFd= socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(socketFd,-1,"socket");

    struct sockaddr_in serverAddr;
    serverAddr.sin_family=AF_INET;
    serverAddr.sin_port=htons(12349);
    /* inet_pton(AF_INET,"127.0.0.1",(void *)&serverAddr.sin_addr); */
    inet_pton(AF_INET,"192.168.56.129",(void *)&serverAddr.sin_addr);

    int connectRet=connect(socketFd,(struct sockaddr*)&serverAddr,sizeof(serverAddr));
    ERROR_CHECK(connectRet,-1,"connect");
//    int cnt=0;
//    while(cnt<3&&login(socketFd)){
//        printf("密码错误\n");
//        cnt++;
//    }
//    if(cnt==3){
//        printf("断开\n");
//    }
    char name[16]={0};
    usr(socketFd,name); 
    char token[41]={0};
    int abc=recv(socketFd,token,sizeof(token),MSG_WAITALL);
    int epollFd=epoll_create(10);
    epollAdd(epollFd,STDIN_FILENO);
    epollAdd(epollFd,socketFd);
    struct epoll_event readySet[1024];
    /* train_t *train=(train_t *)calloc(1,sizeof(train_t)); */
    train_t train1;
    train_t *train=&train1;
    char fileName[16]={0};
    printf("请输入命令\n");
    while(1){
        memset(train,0,sizeof(train_t));
        int readyNum=epoll_wait(epollFd,readySet,1024,-1);
        printf("监听就绪\n");
        for(int i=0;i<readyNum;++i){
            if(readySet[i].data.fd==socketFd){
                commandType ret=0;
                int rdret=recv(socketFd,&ret,sizeof(int),MSG_WAITALL);
                printf("rdret=%d,ret=%d\n",rdret,ret);
                if(ret!=PUTS&&ret!=GETS){
                    if(commandReturn(socketFd,fileName,ret)){
                        return 0;
                    }
                }
            }
            else{
                memset(fileName,0,sizeof(fileName));
                // 用户输入命令,传给服务端
                char buf[1024]={0};
                ERROR_CHECK(fgets(buf,sizeof(buf),stdin),NULL,"fgets");
                buf[strlen(buf)-1]='\0';
                int cmdRret=commandRecognition(buf,train);
                strcpy(fileName,train->argv);
                /* printf("客户端主线程fd=%d\n",socketFd); */
                printf("%d,%d,%s\n",train->command,train->argvSize,train->argv);
                if(cmdRret==-1){
                    continue;
                }
                else if(cmdRret==PUTS){
                    // 创建新线程
                    printf("创建上传线程前\n");
                    pthread_t tid;
                    shareRes_t shareRes;
                    strcpy(shareRes.fileName,train->argv);
                    strcpy(shareRes.name,name);
                    strcpy(shareRes.token,token);
                    THREAD_ERROR_CHECK(pthread_create(&tid,NULL,threadFunc1,&shareRes),"上传线程");
                }
                else if(cmdRret==GETS){
                    // 创建新线程
                    printf("创建下载线程前\n");
                    pthread_t tid;
                    shareRes_t shareRes;
                    strcpy(shareRes.fileName,fileName);
                    strcpy(shareRes.name,name);
                    strcpy(shareRes.token,token);
                    THREAD_ERROR_CHECK(pthread_create(&tid,NULL,threadFunc2,&shareRes),"下载线程");
                }
                else{
                    send(socketFd,train,sizeof(train_t),0);
                    /* sleep(10); */
                }
            }
        }
    }
    /* printf("before recv in client\n"); */
    /* recvFileFromWorker(socketFd); */
    /* printf("after recv in client\n"); */
    close(socketFd);
    return 0;
}
